package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.BussItemT;
import com.haisen.dataobject.entity.Customer;
import com.haisen.dataobject.mapper.base.BussItemTMapper;
import com.haisen.dataobject.mapper.base.CustomerMapper;
import com.haisen.dataobject.query.CustomerQuery;
import com.haisen.poi.basic_poi.BigExcelUtil;
import com.haisen.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public void create(Customer customer) {
        customerMapper.insertSelective(customer);
    }

    @Override
    public void update(Customer customer) {
      customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<Customer> example = Weekend.of(Customer.class);
        WeekendCriteria<Customer,Object> criteria = example.weekendCriteria();
        criteria.andIn(Customer::getId,Arrays.asList(ids));
        customerMapper.deleteByExample(example);
    }

    @Override
    public List<Customer> findAll() {
        return customerMapper.selectAll();
    }

    @Override
    public PageResultSet<Customer> findByPage(CustomerQuery customerQuery) {
        if (!StringUtils.isEmpty(customerQuery.getOrderBy())){
            PageHelper.orderBy(customerQuery.getOrderBy());
        }
        Weekend<Customer> example = Weekend.of(Customer.class);
        WeekendCriteria<Customer,Object> criteria = example.weekendCriteria();

        if (!StringUtils.isEmpty(customerQuery.getUsername())){
            criteria.andEqualTo(Customer :: getUsername,customerQuery.getUsername());
        }
        if (!StringUtils.isEmpty(customerQuery.getPhone())){
            criteria.andEqualTo(Customer :: getPhone,customerQuery.getPhone());
        }
        if (!StringUtils.isEmpty(customerQuery.getStatus())){
            //字符串分割,
            List<String> statusList = Arrays.asList(customerQuery.getStatus().split(","));
            criteria.andIn(Customer::getStatus,statusList);
        }
        //设置页偏移量和数量
        PageHelper.offsetPage(customerQuery.getOffset(), customerQuery.getLimit());
         List<Customer> customerList = customerMapper.selectByExample(example);
        long total = customerMapper.selectCountByExample(example);
        PageResultSet<Customer> resultSet = new PageResultSet<>();
        resultSet.setRows(customerList);
        resultSet.setTotal(total);
        return resultSet;
    }

    /**
     * 批量插入客户信息
     */
    @Override
    public void createBatch(List<Customer> customerList) {
        InsertBatch<Customer> insertBatch = new InsertBatch<>();
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        MyMapper<Customer> myMapper = (MyMapper)sqlSession.getMapper(CustomerMapper.class);
        insertBatch.insertBatch(sqlSession,myMapper,customerList);
    }

    @Override
    public void importExcel2DataBase(String pathFile) {
        BigExcelUtil bigExcelUtil = BigExcelUtil.getInstance();
        List<Object> objs =  bigExcelUtil.importExcel2Obj(pathFile,Customer.class);
        List<Customer> customerList = objs.stream().map( o ->{
            return (Customer)o;
        }).collect(Collectors.toList());
        createBatch(customerList);
    }
}
